﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Expressions;

namespace NMA.Web.Areas.Admin.Controllers
{
    using NMA.Domain.Model.Service.Application;
    using NMA.Infrastructure.DBC;
    using NMA.Domain.Model.DTO;
    using NMA.Web.Areas.Admin.Models;
    using NMA.Infrastructure.Configuration;
    using NMA.Web.Core.PathUtility;
    using NMA.Web.Controllers;
    using NMA.Web.Areas.Admin.Models.SearchCriteria;
    using NMA.Domain.Model;
    using NMA.Infrastructure.NHibernate.DomainObject;

    public class CategoryController : BaseController
    {
        //private readonly ICategoryFacade _categoryFacade;
        //private readonly IConfigurationManager _configManager;

        private const string PAGE_SIZE = "PageSize";
        private readonly string _searchURL = string.Empty;

        public ICategoryFacade CategoryFacade { get; private set; }
        public IConfigurationManager ConfigurationManager { get; private set; }
        public IResolvePath ResolvePathUtility { get; private set; }

        public CategoryController(ICategoryFacade categoryFacade, IConfigurationManager configManager, IResolvePath resolvePath)
        {
            CategoryFacade = categoryFacade;
            ConfigurationManager = configManager;
            ResolvePathUtility = resolvePath;

            _searchURL = ResolvePathUtility.ToAbsolute("~/Admin/Category/Search/{page}");

            if (HttpContext != null)
                if(string.IsNullOrEmpty(HttpContext.User.Identity.Name) == false)
                    CategoryFacade.UserName = HttpContext.User.Identity.Name;
        }
        
        public ActionResult Index()
        {
            Check.Assert(CategoryFacade != null, "Category facade is null");
            Check.Assert(ConfigurationManager != null, "Configuration manager is null");

            IList<CategoryDTO> categories = CategoryFacade.All().ToList<CategoryDTO>();
            CategoryIndexModel model = new CategoryIndexModel();

            var pageSize = Convert.ToInt32(ConfigurationManager.AppSettings.Get(PAGE_SIZE));
            PagingModel pagingModel = new PagingModel(1, pageSize, categories.Count, _searchURL, null);
            model.PagingModel = pagingModel;

            if (categories != null)
            {
                model.Categories = categories;
                return View(model);
            }
            else
            {
                return View(model);
            }
        }

        [HttpPost]
        public ActionResult Search(int page, CategorySearchCriteria model)
        {
            Check.Assert(CategoryFacade != null, "Category facade is null");
            Check.Assert(ConfigurationManager != null, "Configuration manager is null");
            Check.Assert(page > 1, "Page Index is less than zero");
            Check.Assert(model != null, "Criteria model is null");

            var year = this.HttpContext.Request.Form["criteria.CreatedDate.Year"];
            var month = this.HttpContext.Request.Form["criteria.CreatedDate.Month"];
            var day = this.HttpContext.Request.Form["criteria.CreatedDate.Day"];

            DateTime selectedDate;
            if (DateTime.TryParse(string.Format("{0}/{1}/{2}", year, month, day), out selectedDate))
            {
                model.CreatedDate = selectedDate;
            }
            else
            {
                if (day != "0" || month != "0" || year != "0")
                {
                    return this.ResponseError("Invalid date!");
                }
            }

            var pageSize = Convert.ToInt32(ConfigurationManager.AppSettings.Get(PAGE_SIZE));

            //var result = CategoryFacade.All(page, pageSize, x=>x.Id

            return null;
        }

        #region private func

        //private IList<CategoryDTO> GetSearchModel(int pageIndex,
        //    int pageSize,
        //    CategorySearchCriteria model)
        //{
        //    Expression<Func<ICategory, bool>> exp;

        //    var result = CategoryFacade.All(
        //        pageIndex, 
        //        pageSize, 
        //        x=> {
        //            x.Id.CompareTo(model.Id) ||
        //        }
        //    )
        //}

        //private Expression<Func<ICategory, bool>> BuildExpression(CategorySearchCriteria model)
        //{
        //    Expression<Func<ICategory, bool>> exp = null;
        //    ICategory cat = new Category();
        //    cat.Id = model.Id;


        //    if (model.Id != null)
        //    {
        //        if(!string.IsNullOrEmpty(model.Name))
        //            if(!string.IsNullOrEmpty(model.CreatedUser))
        //                if(model.CreatedDate != null)
        //                    exp = 
        //    }

        //    return exp;
        //}

        #endregion
    }
}